﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;
using System.Diagnostics;
namespace DBShell
{
    class Program
    {
        public const string RUN_TYPE = "Release";
        [DllImport(@"..\..\..\"+RUN_TYPE+@"\DBMS.dll", EntryPoint = "mainInitDB", CharSet = CharSet.Ansi)]
        public static extern void mainInitDB();
        [DllImport(@"..\..\..\" + RUN_TYPE + @"\DBMS.dll", EntryPoint = "parseExe", CharSet = CharSet.Ansi)]
        public static extern void parseExe(StringBuilder cmd, ref StringBuilder result);
        [DllImport(@"..\..\..\" + RUN_TYPE + @"\DBMS.dll", EntryPoint = "mainUseDB", CharSet = CharSet.Ansi)]
        public static extern void mainUseDB(StringBuilder cmd);
        [DllImport(@"..\..\..\" + RUN_TYPE + @"\DBMS.dll", EntryPoint = "mainCloseDB", CharSet = CharSet.Ansi)]
        public static extern void mainCloseDB();
        [DllImport(@"..\..\..\" + RUN_TYPE + @"\DBMS.dll", EntryPoint = "mainImportData", CharSet = CharSet.Ansi)]
        public static extern void mainImportData(StringBuilder filename,StringBuilder tablename);

        static StringBuilder result = new StringBuilder();
        
        static void Main(string[] args)
        {
            //可以在此处设定断点，然后F11跟入上面的dll工程
            Console.TreatControlCAsInput = false;
            Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
            mainInitDB();
            CmdOrganizer co = new CmdOrganizer(Console.Out);
            string input = "";
            input = System.Console.ReadLine();
            while (input != "exit")
            {
                co.addBuffer(input.Trim());
                input = System.Console.ReadLine();
            }
            mainCloseDB();
        }
        
        static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            mainCloseDB();
            Console.WriteLine("ber~");
        }

        public static void RunSystemCmd(string cmd,CmdOrganizer co,TextWriter sw)
        {
            string filename;
            switch(cmd)
            {
                case "run":
                    co.addBuffer("");
                    StringBuilder build = new StringBuilder();
                    build = new StringBuilder(co.GetOneCmd().Trim());
                    while(build.Length!=0)
                    {
                        parseExe(build, ref result);
                        if(result!=null)
                        {
                            Console.WriteLine(result.ToString());
                        }
                        result = new StringBuilder();
                        build = new StringBuilder(co.GetOneCmd().Trim());
                    }
                    break;
                case "cls":
                    Console.Clear();
                    break;
                case "import":
                    sw.WriteLine("Please input the insert file:");
                    filename = Console.ReadLine();
                    sw.WriteLine("Please input the insert table:");
                    string tablename = Console.ReadLine();
                    StringBuilder arg1 = new StringBuilder(filename);
                    StringBuilder arg2 = new StringBuilder(tablename);
                    Stopwatch time = new Stopwatch();
                    time.Start();
                    mainImportData(arg1,arg2);
                    time.Stop();
                    Console.WriteLine("Importing {0} finished, spend time {1} s",filename,time.ElapsedMilliseconds/1000.0);
                    break;
                case "execute":
                    sw.WriteLine("Please input the script file:");
                    filename = Console.ReadLine();
                    if (!File.Exists(filename))
                    {
                        Console.WriteLine("No such script file");
                        break;
                    }
                    FileStream fs = File.Open(filename,FileMode.Open);
                    StreamReader sr = new StreamReader(fs,Encoding.Default);
                    while(!sr.EndOfStream)
                    {
                        co.addBuffer(sr.ReadLine());
                    }
                    co.addBuffer("run");
                    break;
                case "usedb":
                    sw.WriteLine("Please input the db name:");
                    string dbname = Console.ReadLine();
                    mainUseDB(new StringBuilder(dbname));
                    break;
                case "closedb":
                    mainCloseDB();
                    break;
                default: break;

            }
        }
    }
}
